package com.lry.basic.nio.mapped;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class Frame {

    private byte start = 0x10;

    private int id;

    private int len;

    private String msg;

    private byte end = 0x16;

    public Frame(int id,String msg) {
        assert msg!=null;
        this.id = id;
        this.len = msg.length();
        this.msg = msg;
    }



    public byte[] toBytes(){
        ByteBuf buf = Unpooled.buffer(10+len);
        buf.writeByte(start);
        buf.writeInt(id);
        buf.writeInt(len);
        buf.writeBytes(msg.getBytes());
        buf.writeByte(end);
        return buf.array();
    }



}
